package com.ego.service.impl;

import com.ego.exception.EgoException;
import com.ego.exception.ExceptionEnum;
import com.ego.mapper.SpecificationMapper;
import com.ego.pojo.Specification;
import com.ego.service.SpecService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @author Ares
 * @date 2020-11-25
 */
@Service
@Slf4j
public class SpecServiceImpl implements SpecService {
    @Resource
    private SpecificationMapper specificationMapper;

    @Override
    @Transactional(readOnly = true)
    public Specification selectSpecByCid(Long cid) {
        Specification specification = null;
        try {
            specification = specificationMapper.selectById(cid);
            if(specification==null){
                EgoException.error(log,ExceptionEnum.NULL_SPEC_PARAM);
            }
        } catch (Exception e) {
            EgoException.error(log,ExceptionEnum.SPEC_PARAM_NOT_FOUND,e);
        }
        return specification;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertSpec(Specification specification) {
        try {
            specificationMapper.insert(specification);
        } catch (Exception e) {
            EgoException.error(log,ExceptionEnum.SPEC_PARAM_CREATE_FAILED,e);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateSpec(Specification specification) {
        specificationMapper.updateById(specification);
    }
}
